package com.server.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.stereotype.Controller;
import io.swagger.annotations.Api;
import com.server.util.Result;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import com.server.entity.UpmsOrganization;
import com.server.service.IUpmsOrganizationService;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.server.util.BaseController;

/**
 * <p>
 * 组织 控制器
 * </p>
 *
 * @author
 * @since 2020-04-03
 */
@RestController
@Controller
@AllArgsConstructor
@Slf4j
@Api(value = "组织管理", tags = "组织管理")
@RequestMapping("/admin/organization")
public class AdminUpmsOrganizationController extends BaseController {

    private IUpmsOrganizationService upmsOrganizationService;


    /**
     * 查询列表
     *
     * @author
     * @Date 2020-04-03
     */
    @GetMapping("/getListPage")
    @ApiOperation(value = "查询列表")
    @ResponseBody
    public Result<List<UpmsOrganization>> listPage(@ApiParam(name = "name", value = "组织名称", required = false)@RequestParam(required = false) String name,
                                                   @ApiParam(name = "curPage", value = "当前页（不传入则默认1）", required = false)@RequestParam(required = false,defaultValue = "1") long curPage,
                                                   @ApiParam(name = "pageSize", value = "当前页大小（不传入则默认10）", required = false)@RequestParam(required = false,defaultValue = "10") long pageSize) {
        Page<UpmsOrganization> page = new Page<>(curPage,pageSize);
        return upmsOrganizationService.findPage(page,name);
    }

    /**
     * 根据id查询详情
     *
     * @author
     * @Date 2020-04-03
     */
    @GetMapping("/findById")
    @ApiOperation(value = "根据id查询详情")
    @ResponseBody
    public Result<UpmsOrganization> findById(@ApiParam(name = "id", value = "组织id", required = true) @RequestParam String id) {
        return Result.returnData(upmsOrganizationService.findById(id));
    }

    /**
     * 新增
     *
     * @author
     * @Date 2020-04-03
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增")
    @ResponseBody
    public Result add(@ApiParam(name = "entity", value = "新增对象")@Valid @RequestBody UpmsOrganization entity) {
        if(upmsOrganizationService.count(new QueryWrapper<UpmsOrganization>().lambda().eq(UpmsOrganization::getName,entity.getName())
                )>0){
            return Result.error("组织名称不能重复");
        }
        return upmsOrganizationService.add(entity);
    }

    /**
     * 修改
     *
     * @author
     * @Date 2020-04-03
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    @ResponseBody
    public Result update(@ApiParam(name = "entity", value = "修改对象")@Valid @RequestBody UpmsOrganization entity) {
        if(entity.getOrganizationId()==null){
            return Result.error("修改组织id不能为空");
        }
        if(upmsOrganizationService.count(new QueryWrapper<UpmsOrganization>().lambda().eq(UpmsOrganization::getName,entity.getName())
                .ne(UpmsOrganization::getOrganizationId,entity.getOrganizationId()))>0){
            return Result.error("组织名称不能重复");
        }
        UpmsOrganization old =  upmsOrganizationService.getById(entity.getOrganizationId());
        if(old==null){
            return Result.error("找不到对应记录");
        }
        BeanUtil.copyProperties(entity,old);
        return  upmsOrganizationService.update(old);
    }

    /**
     * 批量删除
     *
     * @author
     * @Date 2020-04-03
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "批量删除")
    @ResponseBody
    public Result delete(@ApiParam(name = "ids", value = "批量删除", required = true)@Valid @RequestParam String ids) {
        return upmsOrganizationService.delete(ids)?Result.ok("操作成功"):Result.error("操作失败");
    }

}
